package com.jxc.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jxc.dao.BaseDaoI;
import com.jxc.model.po.Resource;
import com.jxc.model.po.Role;
import com.jxc.model.po.RoleResource;
import com.jxc.model.po.User;
import com.jxc.model.vo.DataGrid;
import com.jxc.model.vo.VRole;
import com.jxc.service.ResourceServiceI;
import com.jxc.service.RoleServiceI;

@Service("roleService")
public class RoleServiceImpl implements RoleServiceI {
	@Autowired
	private BaseDaoI<Role> roleDao;
	@Autowired
	private BaseDaoI<RoleResource> roleResourceDao;
	@Autowired
	private BaseDaoI<Resource> resourceDao;
	@Autowired
	private ResourceServiceI resourceService;
	@Autowired
	private BaseDaoI<User> userDao;

	@Override
	public DataGrid datagrid(VRole vRole) {
		DataGrid dg = new DataGrid();
		dg.setRows(this.changeModel(this.find(vRole)));
		dg.setTotal(this.total(vRole));
		return dg;
	}

	private List<VRole> changeModel(List<Role> roles) {
		List<VRole> vRoles = new ArrayList<VRole>();
		if (roles != null && roles.size() > 0) {
			for (Role role : roles) {
				VRole vRole = new VRole();
				BeanUtils.copyProperties(role, vRole);

				List<RoleResource> resources = role.getRoleResources();
				String resourceIds = "";
				String resourceNames = "";
				boolean b = false;
				if (resources != null && resources.size() > 0) {
					for (RoleResource roleResource : resources) {
						Resource resource = roleResource.getResource();
						if (resource != null) {
							if (b) {
								resourceIds += ",";
								resourceNames += ",";
							}
							resourceIds += resource.getId();
							resourceNames += resource.getName();
							b = true;
						}
					}
				}
				vRole.setResourceIds(resourceIds);
				vRole.setResourceNames(resourceNames);
				vRoles.add(vRole);
			}
		}
		return vRoles;
	}

	private List<Role> find(VRole vRole) {
		String hql = "from Role t where isDeleted=false ";

		List<Object> values = new ArrayList<Object>();
		hql = addWhere(vRole, hql, values);

		if (vRole.getSort() != null && vRole.getOrder() != null) {
			hql += " order by " + vRole.getSort() + " " + vRole.getOrder();
		}
		return roleDao.find(hql, values, vRole.getPage(), vRole.getRows());
	}

	private Long total(VRole vRole) {
		String hql = "select count(*) from Role t where isDeleted=false ";
		List<Object> values = new ArrayList<Object>();
		hql = addWhere(vRole, hql, values);
		return roleDao.count(hql, values);
	}

	private String addWhere(VRole vRole, String hql, List<Object> values) {
		if (vRole.getName() != null && !vRole.getName().trim().equals("")) {
			hql += " and t.name like ? ";
			values.add("%%" + vRole.getName().trim() + "%%");
		}
		if(vRole.getType()!=null){
			hql += " and t.type = ? ";
			values.add(vRole.getType());
		}if(vRole.getUid()!=null){
			hql += " and t.user.id = ? ";
			values.add(vRole.getUid());
		}
		return hql;
	}

	@Override
	public List<VRole> combobox(VRole vRole) {
		List<VRole> result = new ArrayList<VRole>();
		result.addAll(changeModel(find(vRole)));
		return result;
	}

	@Override
	public void delete(String ids) {
		if (ids != null) {
			for (String id : ids.split(",")) {
				if (!id.trim().equals("0")) {
					Role role = roleDao.get(Role.class, Long.parseLong(id
							.trim()));
					if (role != null) {
						role.setIsDeleted(true);
						roleDao.update(role);
					}
				}
			}
		}

	}

	@Override
	public void editRoleInfo(VRole vRole) {
		// TODO Auto-generated method stub

	}

	@Override
	public void save(VRole vRole) {
		Role role = new Role();
		BeanUtils.copyProperties(vRole, role);
		if(vRole.getUid()!=null){
			User u = userDao.get(User.class, vRole.getUid());
			role.setUser(u);
		}
		role.setIsDeleted(false);
		this.roleDao.save(role);
		saveRoleAuth(vRole, role);

	}

	@Override
	public void update(VRole vRole) {
		Role role = roleDao.get(Role.class, vRole.getId());
		BeanUtils.copyProperties(vRole, role, new String[] { "id" });
		this.roleDao.update(role);
		saveRoleAuth(vRole, role);
	}

	private void saveRoleAuth(VRole vRole, Role role) {
		roleDao.executeHql("delete RoleResource t where t.role = ?",
				new Object[] { role });
		System.out.println(vRole.getResourceIds());
		if (vRole.getResourceIds() != null) {
			for (String id : vRole.getResourceIds().split(",")) {
				RoleResource roleResource = new RoleResource();
				roleResource.setResource(resourceDao.get(Resource.class, Long
						.valueOf(id.trim())));
				roleResource.setRole(role);
				roleResourceDao.save(roleResource);
			}
		}
		resourceService.refresh(null);
	}

	@Override
	public void refresh() {
		resourceService.removeandrefresh();
		
	}
}
